package com.system.demo.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.system.demo.dao.IMemberDao;
import com.system.demo.dto.MemberDTO;
import com.system.demo.model.Member;


@Service
@Transactional
public class MemberService implements IMemberService {
	
	@Resource
	private IMemberDao memberDao;

	@Override
	public Map findAll(MemberDTO dto) {
		Pageable pageable = PageRequest.of(dto.getPage()-1, dto.getSize(), Sort.by(Direction.DESC, "regdate"));
		Page<Member> page = memberDao.findAll(getWhereClause(dto), pageable);
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("code", 0);
		map.put("count", memberDao.count(getWhereClause(dto)));
		map.put("data", page.getContent());
		return map;
	}

	@Override
	public void save(Member m) {
		memberDao.save(m);
	}

	
	//条件查询
	private Specification<Member> getWhereClause(MemberDTO dto){
		return new Specification<Member>() {
			
			@Override
			public Predicate toPredicate(Root<Member> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				
				List<Predicate> list = new ArrayList<Predicate>();
				
				if(dto.getCategory()==0) {//学生
					if(dto.getDepartment()!=null && ""!=dto.getDepartment()) {//系
						list.add(criteriaBuilder.equal(root.<String>get("department"), dto.getDepartment()));
					}
					if(dto.getProfession()!=null && ""!=dto.getProfession()) {//专业
						list.add(criteriaBuilder.equal(root.<String>get("profession"), dto.getProfession()));
					}
					if(dto.getGrade()!=null && ""!=dto.getGrade()) {//年级
						list.add(criteriaBuilder.equal(root.<String>get("grade"), dto.getGrade()));
					}
				}
				
				if(dto.getName()!=null && ""!=dto.getName()) {//姓名模糊查询
					list.add(criteriaBuilder.like(root.<String>get("name"), "%"+dto.getName()+"%"));
				}
				
				if(dto.getCardNo()!=null && ""!=dto.getCardNo()) {//会员卡号
					list.add(criteriaBuilder.equal(root.get("card_no").as(String.class), dto.getCardNo()));
				}
				if(dto.getPhone()!=null && ""!=dto.getPhone()) {//手机号
					list.add(criteriaBuilder.equal(root.get("phone").as(String.class), dto.getPhone()));
				}
				if(dto.getStartTime()!=null) {//入会时间
					list.add(criteriaBuilder.greaterThanOrEqualTo(root.get("regdate").as(Date.class), dto.getStartTime()));
				}
				if(dto.getEndTime()!=null) {//入会时间
					list.add(criteriaBuilder.lessThanOrEqualTo(root.get("regdate").as(Date.class), dto.getEndTime()));
				}
				if(dto.getStatus()>0) {
					list.add(criteriaBuilder.lt(root.<Integer>get("status"), dto.getStatus()));
				}
				
				Predicate[] pre = new Predicate[list.size()];
				return query.where(list.toArray(pre)).getRestriction();
			}
		} ;
	}



	
}
